package org.smartboot.flow.core;

import org.smartboot.flow.core.util.AssertUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qinluo
 * @date 2022-11-25 21:15:22
 * @since 1.0.0
 */
public class ExecutionListenerRegistry {

    private static final List<ExecutionListener> REGISTERED = new ArrayList<>();

    public synchronized static void register(ExecutionListener listener) {
        AssertUtil.notNull(listener, "listener must not be null");
        if (!REGISTERED.contains(listener)) {
            REGISTERED.add(listener);
            listener.doAfterRegister();
        }
    }

    public synchronized static void unregister(ExecutionListener listener) {
        AssertUtil.notNull(listener, "listener must not be null");
        if (REGISTERED.remove(listener)) {
            listener.doAfterUnregister();
        }
    }

    public static List<ExecutionListener> getRegistered() {
        return new ArrayList<>(REGISTERED);
    }
}
